//
// Created by choumei on 2021/9/12.
//

#include "ReadThread.h"

[[noreturn]] void ReadThread::run() {
    while (true) {
        struct MsgInfo {
            int32_t msgSize;
            int32_t msgType;
            int32_t msgId;
        };
        MsgInfo info;
        char *data = nullptr;
        buffer->read((char *) &info.msgSize, 4);
        buffer->read((char *) &info.msgType, 4);
        buffer->read((char *) &info.msgId, 4);
        data = new char[info.msgSize];
        buffer->read(data, info.msgSize);
        int tempSize = 0;
        int ret;
        while (tempSize < info.msgSize) {
             ret = dev->write(data + tempSize, info.msgSize);
            tempSize += ret;
            info.msgSize -= ret;
        }
        qDebug() << ret;
        delete data;
    }

}

ReadThread::ReadThread(MyBuffer *buffer, int sockfd, QIODevice *dev) {
    this->buffer = buffer;
    this->sockfd = sockfd;
    this->dev = dev;
}

